<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Models\User;
use App\Transformers\UserTransformer;

class FollowersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $type = (int) $request->input('listType', 0);
        $user_id = $request->input('user_id', 0);
        if( $user_id && $user = User::find($user_id)) {
            $query = $user;
        } else if ($user_id) {
            return $this->response->error('用户不存在', 404);
        } else {
            $query = $this->user();
        }

        if ($type < 1) {
            $follows = $query->followers()->paginate(10);
        } else {
            $follows = $query->followings()->paginate(10);
        }
        return $this->response->paginator($follows, new UserTransformer);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(User $user, Request $request)
    {
        $this->user()->follow($user->id);
        return $this->response->created();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user, Request $request)
    {
        $this->user()->unfollow($user->id);
        return $this->response->noContent();
    }
}
